var util = require('../../utils/util.js');
const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
Page({

  data: {
 
  },
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight);
        var brandarr = (wx.getStorageSync('brandstr') + '').split(';');
        for(var i=0;i<brandarr.length;i++){
          if(brandarr[i]==''){
            brandarr.splice(i, 1);
          }
        }
        that.setData({
          scrollHeight: res.windowHeight - 276,
          brandstr: wx.getStorageSync('brandstr') + '',
          brandarr:brandarr
        })
      },
    })
  },
  searchbrand:function(){
    var that = this;
    // 获取成员信息
    var datas = {};
    datas.pagesize=100;
    datas.pageindex=1;
    datas.where = "BrandName like '%" + that.data.key + "%' or Keywords like '%" + that.data.key +"%'";
    var datacall = function (res) {
      console.log(res);
      if (res.data.data.length != 0) {
        var brandinfo =res.data.data;
        var str = wx.getStorageSync('brandstr')+'';
        for(var i=0;i<brandinfo.length;i++){
          brandinfo[i].hasadd=0;
          if (str.indexOf(brandinfo[i].BrandName) != -1){
            brandinfo[i].hasadd = 1;
          }
        }
        that.setData({
          brandinfo:res.data.data
        })
      }else{
        that.setData({
          brandinfo: []
        })
      }
    }
    var res2 = post.getdata("/BO/BA_Brand/Query", datas, datacall);
  },
  getkey: function (e) {
    this.setData({
      key: e.detail.value
    })
  },
  getkey2: function (e) {
    this.setData({
      key2: e.detail.value
    })
  },
  addbrand:function(){
    var that =this;
    var brandstr = wx.getStorageSync('brandstr');
    brandstr = brandstr + that.data.key2 + ";";
    wx.setStorageSync('brandstr', brandstr);
    var brandarr = (brandstr + '').split(';');
    for (var i = 0; i < brandarr.length; i++) {
      if (brandarr[i] == '') {
        brandarr.splice(i, 1);
      }
    }
   
    that.setData({
      brandstr: brandstr,
      brandarr: brandarr,
      key2: ''
    })
  },
  reset:function(){
    var that = this;
    wx.setStorageSync('brandstr', '');
    that.setData({
      brandstr: '',
      brandarr: '',
      key2: '',
      brandinfo: '',
      key:'',
    })
  },
  toadd:function(e){
    var that =this;
    console.log(e.currentTarget.dataset.index);
    var idx = e.currentTarget.dataset.index;
    var datainfo =that.data.brandinfo;
    datainfo[idx].hasadd =1;
    var brandstr = wx.getStorageSync('brandstr');
    brandstr = brandstr + e.currentTarget.dataset.brand + ";" ;
    wx.setStorageSync('brandstr', brandstr);
    var brandarr = (brandstr + '').split(';');
    for (var i = 0; i < brandarr.length; i++) {
      if (brandarr[i] == '') {
        brandarr.splice(i, 1);
      }
    }
    that.setData({
      brandinfo: datainfo,
      brandstr:brandstr,
      brandarr:brandarr
    })
  },
  toremove:function(e){
    var that = this;
    var idx = e.currentTarget.dataset.index;
    var datainfo = that.data.brandinfo;
    datainfo[idx].hasadd = 0;
    var brandstr = wx.getStorageSync('brandstr');
    // brandstr = brandstr + ";" + e.currentTarget.dataset.brand;
    brandstr = brandstr.replace(e.currentTarget.dataset.brand+';','');
    wx.setStorageSync('brandstr', brandstr);
    var brandarr =brandstr.split(';');
    for (var i = 0; i < brandarr.length; i++) {
      if (brandarr[i] == '') {
        brandarr.splice(i, 1);
      }
    }
    that.setData({
      brandinfo: datainfo,
      brandstr: brandstr,
      brandarr:brandarr
    })
  }
})